package q;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ch6_22 extends JFrame implements ItemListener{
    JPanel panel;
    JComboBox<String> comboBox;
    JList list;
    Color c;
    JTextPane textPane;
    public ch6_22() {
        panel = new JPanel();
        String[] str={"B","R","G","Y"};
        comboBox = new JComboBox<>(str);
        String[] sizestr={"1","2","3","4"};
        list=new JList(sizestr);
        list.setFixedCellHeight(25);
        list.setFixedCellWidth(25);;
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        textPane=new JTextPane();
        textPane.setText("文本");
        panel.add(comboBox);
        panel.add(list);
        panel.add(textPane);
        panel.add(panel);
        this.add(panel);
        this.setVisible(true);
        this.setSize(500,500);
        comboBox.addItemListener(this);
    }
    public static void main(String[] args) {
        new ch6_22();
    }
    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getSource()==comboBox){
            if(comboBox.getSelectedItem().equals("B")){
                c= Color.BLUE;
            }else if(comboBox.getSelectedItem().equals("R")){
                c=Color.RED;
            }else if(comboBox.getSelectedItem().equals("G")){
                c= Color.GREEN;
            }else if(comboBox.getSelectedItem().equals("Y")){
                c= Color.yellow;
            }
            textPane.setText("文本"+c);
            textPane.setForeground(c);
        }
    }
}
